package com.sanchunhui.psc.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseApplication

/**
 * @Author zyx
 * @Date 2021/4/14-16:19
 * @Dscribe 我的界面适配器
 */

class MyViewAdapter(val context: FragmentActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        val TYPE_HEAD = 0
        val TYPE_SET = 1
    }
    /*
    * 不同position对应不同类型
    * */
    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return TYPE_HEAD
        } else if (position == 1) {
            return TYPE_SET
        }
        return super.getItemViewType(position)
    }

    var mDatas : ArrayList<String> = ArrayList() //数据源
    fun setData(data : ArrayList<String>) {
        this.mDatas = data
        notifyDataSetChanged()
    }

    // 在该方法中创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when(viewType) {
            TYPE_HEAD -> return HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_my_head, parent, false))
            TYPE_SET -> return SetViewHolder(LayoutInflater.from(context).inflate(R.layout.item_my_set, parent, false))
            else -> return HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_my_head, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return 2
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        when (viewType) {
//            TYPE_HEAD -> (holder as HeadViewHolder).bindData(mData[position])
//            TYPE_SET -> (holder as SetViewHolder).bindData(mData[position])
        }
    }

    // 内部类 继承
    inner class HeadViewHolder(item : View) : RecyclerView.ViewHolder(item) {

        val iv_user_head : ImageView
        val tv_name : TextView

        init {
            iv_user_head = item.findViewById(R.id.iv_user_head)
            tv_name = item.findViewById(R.id.tv_name)
            //展示登录成功的ui效果
            val user = BaseApplication.mUser
            tv_name.text = user.name.toString()
        }

        fun bindData(data: String) {

        }
    }

    inner class SetViewHolder(item : View) : RecyclerView.ViewHolder(item) {

        fun bindData(data: String) {

        }
    }
}